package com.haoshuang.zhouzhoubang.ui.activity

import android.Manifest
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.fragment.app.Fragment
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.ActivityMemberConfirmSignBinding
import com.haoshuang.zhouzhoubang.model.bean.OrderBean5
import com.haoshuang.zhouzhoubang.ui.dialog.CallDialog
import com.haoshuang.zhouzhoubang.ui.dialog.ShowUploadExampleDialog
import com.haoshuang.zhouzhoubang.ui.vm.MemberConfirmSignViewModel
import com.haoshuang.zhouzhoubang.utils.OSSUploadUtils
import com.haoshuang.zhouzhoubang.utils.PermissionHelper
import com.haoshuang.zhouzhoubang.widget.CustomUploadView
import com.haoshuang.zhouzhoubang.widget.UploadFileBean
import com.lnkj.jialubao.utils.AccountUtils
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnPermissionsInterceptListener
import com.luck.picture.lib.interfaces.OnRequestPermissionListener
import com.luck.picture.lib.interfaces.OnResultCallbackListener
import com.lxj.xpopup.XPopup
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.ktx.getString
import com.mufeng.libs.utils.ktx.runOnUIThread
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.gone
import com.mufeng.libs.utils.view.load
import com.mufeng.libs.utils.view.visible


/**
 * 订单成员确认签到
 */
class MemberConfirmSignActivity :
    BaseVMActivity<MemberConfirmSignViewModel, ActivityMemberConfirmSignBinding>() {

    companion object {
        fun launch(context: Context) {
            context.startPage<MemberConfirmSignActivity>()
        }
    }

    private var orderBean5: OrderBean5? = null
    private var order_id = ""
    // 是否显示工作内容图片
    private var type = 0
    // 工装照片
    private var workClothesImg = ""

    override fun initIntent() {
        order_id = intent.getString("order_id")

    }

    override fun initView(savedInstanceState: Bundle?) {

        binding.titleBar.title = "确认签到"
        binding.titleBar.leftImageView().click { finish() }

        binding.tvUploadExample.clickWithTrigger {
            // 查看上传示例
            // 后期需要先请求接口
            XPopup.Builder(mActivity)
                .asCustom(ShowUploadExampleDialog(context = mActivity, order_id, 1))
                .show()
        }

        binding. ivServiceCustom.clickWithTrigger {
            // 联系客服
            val myBean = AccountUtils.user
            XPopup.Builder(mActivity)
                .asCustom(CallDialog(mActivity, myBean?.master_customer_tel ?: "") {
                    val intent = Intent(Intent.ACTION_DIAL);
                    intent.setData(Uri.parse("tel:${myBean?.master_customer_tel ?: ""}"));
                    startActivity(intent);
                })
                .show()
        }

        binding.tXz.clickWithTrigger {
            if (type == 0) {
                type = 1
                binding.tXz.setImageResource(R.mipmap.icon_xl2)
                binding.ivImage.visible()
            } else {
                type = 0
                binding.ivImage.gone()
                binding.tXz.setImageResource(R.mipmap.icon_xl)
            }
        }

        binding.rivWorkClothesImg.setOnUploadResultListener(object : CustomUploadView.OnUploadResultListener{
            override fun onUploadResult(mediaList: List<UploadFileBean>) {
                workClothesImg = mediaList.firstOrNull()?.url ?:""
            }
        })

        binding.tvStartSign.clickWithTrigger {
            // 开始签到
            if (workClothesImg.isEmpty()){
                toast("请上传工装照片")
                return@clickWithTrigger
            }
            vm.masterOrderSign("id" to order_id,"sign_img" to workClothesImg)
        }

    }

    override fun initData() {
        vm.getMasterOrderBase("id" to order_id)
    }

    override fun startObserve() {

        vm.masterOrderSignData.observeState(this){
            onLoading {showLoading()}
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                setResult(200)
                finish()
            }
        }

        vm.getMasterOrderBaseData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                orderBean5 = it
                binding.apply {
                    ivHead.load(it?.image)
                    ivImage.load(it?.service_con_image, R.mipmap.default_image)
                    tvName.text = it?.store_name
                    tvMaij.text = it?.suk_unique
                }
            }
        }

    }

}